<?php
class UsersController extends AppController {

	var $name = 'Users';

	var $components = array('Auth', 'Session');
	var $uses = array('User', 'Tab', 'Setting');

	function beforeFilter(){
		parent::beforeFilter();
		$this->Auth->fields = array(
				'username' => 'email',
				'password' => 'password'
			);
		$this->Auth->allow('registration', 'login');
	}

	function registration() {
		if (empty ($this->data)) {
			// form view
		}
		elseif (empty($this->data['User']['first_password'])) {
			$this->Session->setFlash('Пароль не может быть пустым.');
		}
		elseif ($this->data['User']['first_password'] != $this->data['User']['second_password']) {
			$this->Session->setFlash('Введённые пароли не совпадают.');
		}
		elseif (strlen($this->data['User']['first_password']) < PASSWORD_LEN) {
			$this->Session->setFlash('Пароль не может быть короче '.PASSWORD_LEN.' символов.');
		}
		else {
			$this->data['User']['password'] = $this->Auth->password($this->data['User']['first_password']);
			$this->data['User']['role'] = 'user';
			$this->data['User']['status'] = 'active';
			if ($this->User->save($this->data)) {
				$data['Tab']['user_id'] = $this->User->id;
				$data['Tab']['position'] = 1;
				$data['Tab']['label'] = 'Общие';
				$this->Tab->save($data);

				$data['Setting']['user_id'] = $this->User->id;
				$data['Setting']['tabs_limit'] = TABS_LIMIT;
				$data['Setting']['links_limit'] = LINKS_LIMIT;
				$this->Setting->save($data);

				$this->Session->setFlash('Чуваааак, ты успешно зареган! Теперь можеш залогиниться!');
				$this->redirect('/');
			}
		}
	}

	function login() {
	}

	function logout() {
		$this->Auth->logout();
		$this->redirect('/');
	}

	private function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	private function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	private function add() {
		if (!empty($this->data)) {
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
	}

	private function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
	}

	private function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	function admin_index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>